package com.brest.managesambafilesharing;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.brest.managesambafilesharing.IsSambaRunning.OnResponseListenerIsSambaRunning;
import com.brest.tools.RunAsRoot;
import com.brest.tools.RunAsRoot.OnResponseListenerRunAsRoot;

public class DummyActivity extends Activity {
	int ToastTime = 2;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LOG.d("DummyActivity - onCreate()");
		handleIntent(this.getIntent());
	}

	protected OnResponseListenerRunAsRoot onResponseListener = new OnResponseListenerRunAsRoot() {
		public void onResponse(String message) {
			/*
			 * AlertDialog.Builder builder = new
			 * AlertDialog.Builder(GeneralHTTPRequestExampleActivity.this);
			 * builder.setPositiveButton("Okay", new
			 * DialogInterface.OnClickListener() { public void
			 * onClick(DialogInterface dialog, int which) { dialog.cancel(); }
			 * }); builder.setMessage("Failure. Message: " + message);
			 * builder.show(); }
			 */
			// Toast.makeText(DummyActivity.this.getApplicationContext(),
			// message, ToastTime).show();
			startActivity(new Intent(
					DummyActivity.this.getApplicationContext(),
					DummyActivity.class).setAction("INTENTINFO_STATUS"));

		}
	};

	protected OnResponseListenerIsSambaRunning onResponseListenerIsSambaRunning = new OnResponseListenerIsSambaRunning() {
		public void onResponse(String message) {
			try {
				Toast.makeText(DummyActivity.this, message, ToastTime).show();

				new MyNotificationMgr().createNotification(
						DummyActivity.this.getApplicationContext(), message);

				Intent intent = new Intent(
						DummyActivity.this.getApplicationContext(),
						MainActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				intent.setAction("INTENTINFO_CLOSE");
				startActivity(intent);

			} catch (Exception e) {
				LOG.e("onPostExecute error : " + e.getMessage());
			}
		}
	};

	private void startSamba() {
		LOG.d("startSamba() ");
		String commands[] = { SambaCmdToolBean.STARTSAMBA };
		new RunAsRoot().start(commands, onResponseListener);
	}

	private void stopSamba() {
		LOG.d("stopSamba() ");
		String commands[] = { SambaCmdToolBean.STOPSAMBA };
		new RunAsRoot().start(commands, onResponseListener);
	}

	protected void handleIntent(Intent intent) {
		String actionName = intent.getAction();
		LOG.d("onNewIntent : " + actionName);
		if ("INTENTINFO_START".equals(actionName)) {
			startSamba();
		}
		if ("INTENTINFO_STOP".equals(actionName)) {
			stopSamba();
		}
		if ("INTENTINFO_STATUS".equals(actionName)
				|| "android.intent.action.MAIN".equals(actionName)) {
			new IsSambaRunning(onResponseListenerIsSambaRunning) {
				protected void onPreExecute() {
					LOG.i("Pre-Execute");
					super.onPreExecute();
					// MainActivity.this.mStatustextView.setText("");
					// MainActivity.this.mProgressBarSearch
					// .setVisibility(View.VISIBLE);
				}
			}.execute("status");
		}
	}

	@Override
	protected void onNewIntent(Intent intent) {
		// TODO Auto-generated method stub
		super.onNewIntent(intent);
		handleIntent(intent);
	}

}
